"""
Case Type   : Timescaledb功能测试
Case Name   : 非时序表show_chunks查询返回值为0
Create At   : 2025/05/13
Owner       : lihongji
Description :
              1.创建表及索引
              2.查看chunk的返回值
              3.清理环境
Expect      :
              1.创建表、索引成功
              2.无chunk信息返回
              3.清理环境成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Timescaledb(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbPGmode')
        self.db_user_node = Node(node='PrimaryDbPGmode')
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)
        select_extension_cmd = f"SELECT * FROM pg_extension;"
        select_extension_result = self.primary_sh.execut_db_sql(
            select_extension_cmd)
        if "timescaledb" not in select_extension_result:
            create_extension_cmd = f"create extension timescaledb;"
            create_extension_result = (
                self.primary_sh.execut_db_sql(create_extension_cmd))
            self.assertIn(self.constant.extension_create_success,
                          create_extension_result, '创建timescaledb扩展失败')

    def test_add_dimension(self):
        text = '----step1: 创建表及索引 expect: 创建表、索引成功----'
        self.log.info(text)
        create_table = """CREATE TABLE PUBLIC.drop_chunk_test1(
        time bigint, temp float8, device_id text);
        CREATE TABLE PUBLIC.drop_chunk_test2(
        time bigint, temp float8, device_id text);
        CREATE TABLE PUBLIC.drop_chunk_test3(
        time bigint, temp float8, device_id text);"""
        create_index = "CREATE INDEX ON drop_chunk_test1(time DESC);"
        create_table_result = self.primary_sh.execut_db_sql(create_table)
        create_index_result = self.primary_sh.execut_db_sql(create_index)
        self.log.info(create_table)
        self.log.info(create_index)
        self.log.info(create_table_result)
        self.log.info(create_index_result)
        self.assertEqual(3, create_table_result.count(
            self.constant.TABLE_CREATE_SUCCESS), '执行失败' + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG,
                      create_index_result, '执行失败' + text)

        text = '----step2: 查看chunk的返回值 expect: 无chunk信息返回----'
        self.log.info(text)
        show_chunks = "SELECT show_chunks();"
        show_chunks_result = self.primary_sh.execut_db_sql(show_chunks)
        self.log.info(show_chunks_result)
        self.assertIn('(0 rows)', show_chunks_result, '执行失败' + text)

    def tearDown(self):
        text = '-----step3:清理环境 expect:成功-----'
        self.log.info(text)
        drop_table = ("drop table PUBLIC.drop_chunk_test1;"
                      "drop table PUBLIC.drop_chunk_test2;"
                      "drop table PUBLIC.drop_chunk_test3")
        drop_table_result = self.primary_sh.execut_db_sql(drop_table)
        self.assertEqual(3, drop_table_result.count(
            self.constant.DROP_TABLE_SUCCESS), '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
